package com.platform.service.impl;

import com.platform.dao.RemitDao;
import com.platform.dao.SysParameterDao;
import com.platform.dao.UserDao;
import com.platform.entity.RemitEntity;
import com.platform.entity.SysParameterEntity;
import com.platform.entity.UserEntity;
import com.platform.service.UserService;
import com.platform.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-16 15:02:28
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private SysParameterDao sysParameterDao;
    @Autowired
    private RemitDao remitDao;

    @Override
    public UserEntity queryObject(Integer id) {
        return userDao.queryObject(id);
    }

    @Override
    public List<UserEntity> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userDao.queryTotal(map);
    }

    @Override
    public int save(UserEntity user) {
        user.setRegisterTime(new Date());
        return userDao.save(user);
    }

    @Override
    public int update(UserEntity user) {
        return userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return userDao.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void remit(Long sysUser) {
        //判断今天能不能打款
        List<RemitEntity>  remitEntityList = remitDao.queryRemits();
        SysParameterEntity sysParameterEntity0 = sysParameterDao.queryObjectByName("remit_interval");
        int mistiming = 86400000;
        if(sysParameterEntity0 != null){
            mistiming = Integer.parseInt(sysParameterEntity0.getParameterValue());
        }
        Date remitTime = remitEntityList.get(0).getRemitTime();
        Date currentDate = new Date();
        if( mistiming > (currentDate.getTime() - remitTime.getTime())){
            throw new RRException("24小时内不可重复操作！");
        }
        //打款
        SysParameterEntity sysParameterEntity = sysParameterDao.queryObjectByName("remit_money");
        BigDecimal balanceFee = null;
        if(sysParameterEntity == null){
            balanceFee = new BigDecimal("0.00");
        } else {
            balanceFee = new BigDecimal(sysParameterEntity.getParameterValue());
        }
        //生成日志
        Map params = new HashMap();
        params.put("userLevelId", 0);
        List<UserEntity> userEntityList = userDao.queryList(params);
        List<RemitEntity> remitEntitys = new ArrayList<RemitEntity>();
        if( userEntityList != null && userEntityList.size() > 0 ){
            for (UserEntity userEntity : userEntityList) {
                RemitEntity remitEntity = new RemitEntity();
                remitEntity.setSysUser(sysUser);
                remitEntity.setNideshopUser(userEntity.getId());
                remitEntity.setRemitTime(currentDate);
                remitEntity.setRemitFee(balanceFee);
                remitEntity.setRemitType(1);
                remitEntity.setRemitStatus(2);
                remitEntitys.add(remitEntity);
            }
        }
        if( remitEntitys != null && remitEntitys.size() > 0 ){
            remitDao.insertByBatch(remitEntitys);
            userDao.updateToBalanceFee(balanceFee);
        }
    }

    @Override
    public List<UserEntity> queryByMobile(String mobile) {
        return userDao.queryByMobile(mobile);
    }
}
